/*****************************************************
*                                                                          
*               COPYRIGHT (c) 2015-2017 Hargic Corporation                 
*                         All Rights Reserved                              
*                                                                          
* The source code contained or described herein and all documents          
* related to the source code ("Material") are owned by Hargic            
* Corporation or its licensors.  Title to the Material remains             
* with Hargic Corporation or its suppliers and licensors.                  
*                                                                          
* The Material is protected by worldwide copyright and trade secret        
* laws and treaty provisions. No part of the Material may be used,         
* copied, reproduced, modified, published, uploaded, posted, transmitted,  
* distributed, or disclosed in any way except in accordance with the       
* applicable license agreement.                                            
*                                                                          
* No license under any patent, copyright, trade secret or other            
* intellectual property right is granted to or conferred upon you by       
* disclosure or delivery of the Materials, either expressly, by            
* implication, inducement, estoppel, except in accordance with the         
* applicable license agreement.                                            
*                                                                          
* Unless otherwise agreed by Hargic in writing, you may not remove or      
* alter this notice or any other notice embedded in Materials by Hargic    
* or Hargic's suppliers or licensors in any way.                           
*                                                                          
** Author: shengda.huang
** Date: 2018-07-14
*
*****************************************************/

/***** Include files ********************************/
#include "rf433_master.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "cc1101.h"

/***** Defines **************************************/

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/

rf433_slv_mgr_t slaves[RF433_SUPPORT_SLAVE_MAX];

/***** Define Prototype of functions ****************/

/**
 * @brief
 * @param  None
 * @retval None
 */
int rf433_find_slv(unsigned short slv_addr)
{
	for (int i=0; i<RF433_SUPPORT_SLAVE_MAX; i++)
	{
		if (slaves[i].alive == 0)
			continue;

		if (i == slv_addr)
			return i;
	}

	return -1;
}

int rf433_register_req_handler(unsigned char *buff, unsigned int len)
{
#define RF433_REGISTER_RESP_MSGLEN		(sizeof(rf433_header_t) + sizeof(rf433_register_resp_t))
	unsigned char data[RF433_REGISTER_RESP_MSGLEN];
	int i, msglen;
	rf433_header_t *header = (rf433_header_t *)buff;
	rf433_register_req_t *body = (rf433_register_req_t *)(buff + sizeof(rf433_header_t));
	rf433_register_resp_t *resp = (rf433_register_resp_t *)(data + sizeof(rf433_header_t));

	printf("%s: enter\n", __func__);

	for (i=0; i<RF433_SUPPORT_SLAVE_MAX; i++)
	{
		if (slaves[i].alive == 0)
			break;
	}
	if (i == RF433_SUPPORT_SLAVE_MAX)
	{
		printf("[ERR]%s: slave is full\n", __func__);
		return -1;
	}

	slaves[i].seq = header->seq++;
	memcpy((unsigned char *)&slaves[i].info, (unsigned char *)body, sizeof(rf433_register_req_t));
	resp->addr = i;
	resp->result = RF433_RST_TYPE_SUSS;
	memcpy(resp->hostid, master_hostid, 3);

	msglen = rf433_create_pkg(data, RF433_REGISTER_RESP_MSGLEN, i, slaves[i].seq, RF433_MSG_SLAVE_REQ_RSP, sizeof(rf433_register_resp_t));
	CC1101_Tx_Packet(data, msglen, ADDRESS_CHECK);
	
	printf("%s: add slave %d\n", __func__, i);
	slaves[i].alive = !0;

	printf("%s: exit\n", __func__);
	return 0;
}

int rf433_register_report_switch_handler(unsigned char *buff, unsigned int len)
{
#define RF433_INDICATION_RESP_MSGLEN		(sizeof(rf433_header_t) + sizeof(uint32_t))
	unsigned char data[RF433_INDICATION_RESP_MSGLEN];
	int msglen;
	rf433_header_t *header = (rf433_header_t *)buff;
	uint32_t *body = (uint32_t *)(buff + sizeof(rf433_header_t));
	uint32_t *resp = (uint32_t *)(data + sizeof(rf433_header_t));

	printf("%s: enter\n", __func__);

	if (header->addr < 0 || header->addr >= RF433_SUPPORT_SLAVE_MAX)
	{
		printf("[ERR]%s: invalid slave addr %d\n", __func__, header->addr);
		return -1;
	}

	if (slaves[header->addr].alive == 0)
	{
		printf("[ERR]%s: slave %d is not alive\n", __func__, header->addr);
		return -2;
	}

	slaves[header->addr].seq = header->seq++;
	printf("%s: get slave %d indication %x\n", __func__, header->addr, *body);

	*resp = RF433_RST_TYPE_SUSS;

	msglen = rf433_create_pkg(data, RF433_INDICATION_RESP_MSGLEN, header->addr, slaves[header->addr].seq, RF433_MSG_SLAVE_REPORT_SWITCH_RSP, sizeof(uint32_t));
	CC1101_Tx_Packet(data, msglen, ADDRESS_CHECK);
	printf("%s: exit\n", __func__);
	return 0;
}

/* End of file */



